package club.zhilin.guli.service.base.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @author zhilin
 * @since 2022/2/27 19:03
 */
@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {
    /**
     * 可选配置
     *
     * @return CookieSerializer
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        //  将 spring session 默认的 cookie key 从Session替换为原生的SessionID
        serializer.setCookieName("JSESSIONID");
        // CookiePath 设置为根路径
        serializer.setCookiePath("/");
        // 配置了相关的正则表达式,可以达到同父域下的单点登录的效果
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        return serializer;
    }
}
